/*
 *  This file was originally created by Ireneusz Grala on 29th of December 2010
 *  e-mail: ireneusz.grala@wp.pl
 *  
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;


namespace SimpleGPSApp.Base
{
    public class Config
    {
        public static string ErrorLogFileName = SimpleGPSApp.Base.Application.StartupPath + "\\errorLog.txt";

        public static string PositionLogFileName = SimpleGPSApp.Base.Application.StartupPath + "\\positions.txt";

        private string fileName = SimpleGPSApp.Base.Application.StartupPath + "\\application.xml";

        private Settings applicationSettings = new Settings();

        public Settings ApplicationSettings
        {
            get { return applicationSettings; }
            set { applicationSettings = value; }
        }

        public void SaveConfig()
        {
            if (File.Exists(fileName))
                File.Delete(fileName);

            FileStream fileStream = new FileStream(fileName, FileMode.Create);

            if (fileStream != null)
            {
                try
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                    serializer.Serialize(fileStream, applicationSettings);
                    fileStream.Close();
                }
                finally
                {
                    IRQBaseFunctions.ErrorLog.WriteError(ErrorLogFileName, "File saving error");

                    if (fileStream != null)
                        fileStream.Close();
                }
            }
            else
                IRQBaseFunctions.ErrorLog.WriteError(ErrorLogFileName, "File creating error");
        }

        public void LoadConfig(Settings settings)
        {
            if (File.Exists(fileName))
            {
                FileStream fileStream = new FileStream(fileName, FileMode.Open);

                if (fileStream != null)
                {
                    try
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                        this.applicationSettings = (Settings)serializer.Deserialize(fileStream);
                        settings = this.applicationSettings;
                        fileStream.Close();
                    }
                    finally
                    {
                        if (fileStream != null)
                            fileStream.Close();
                    }
                }
                else
                {
                    IRQBaseFunctions.ErrorLog.WriteError(ErrorLogFileName, "File opening error");
                }
            }
            else
            {
                IRQBaseFunctions.ErrorLog.WriteError(ErrorLogFileName, "Settings file doesn't exist");
            }
        }
    }


    
    public class Settings
    {
        [XmlElement]
        public string TrackSiteAddress = "http://www.gdziejestiga.waw.pl//position.php";

        [XmlElement]
        public int PositionLogFrequency = 5000;

        [XmlElement]
        public LogType LogingType = LogType.Off;

        [XmlElement]
        public int Zoom = 1;
    }

    public enum LogType : int
    {
        File,
        Server,
        FileAndServer,
        Off,
    }
}
